package com.tencreat.service.mall.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tencreat.common.core.domain.model.ReqIdParam;
import com.tencreat.common.core.domain.model.ReqIdsParam;
import com.tencreat.common.exception.CustomException;
import com.tencreat.common.utils.SecurityUtils;
import com.tencreat.domain.mall.MallCart;
import com.tencreat.domain.mall.vo.ResMallCartItem;
import com.tencreat.domain.mall.dto.MallCartAddDTO;
import com.tencreat.domain.mall.dto.MallCartEditDTO;
import com.tencreat.mapper.mall.MallCartMapper;
import com.tencreat.service.mall.IMallCartService;
import com.tencreat.service.mall.IMallGoodsNormsService;
import com.tencreat.service.mall.IMallGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 购物车商品Service业务层处理
 *
 * @author tencreat
 * @date 2021-08-13
 */
@Service
public class MallCartServiceImpl extends ServiceImpl<MallCartMapper, MallCart> implements IMallCartService {

    private final IMallGoodsService mallGoodsService;
    @Autowired
    private IMallGoodsNormsService mallGoodsNormsService;

    public MallCartServiceImpl(IMallGoodsService mallGoodsService) {
        this.mallGoodsService = mallGoodsService;
    }

    /**
     * 查询购物车商品
     *
     * @param id 购物车商品ID
     * @return 购物车商品
     */
    @Override
    public MallCart selectMallCartById(ReqIdParam id) {
        return baseMapper.selectById(id.getId());
    }

    /**
     * 查询购物车商品列表
     *
     * @param mallCart 购物车商品
     * @return 购物车商品
     */
    @Override
    public List<MallCart> selectMallCartList(MallCart mallCart) {
        return baseMapper.selectList(Wrappers.query(mallCart));
    }

    /**
     * 新增购物车商品
     *
     * @param mallCart 购物车商品
     * @return 结果
     */
    @Override
    public int insertMallCart(MallCart mallCart) {
        mallCart.setCreateTime(LocalDateTime.now());
        return baseMapper.insert(mallCart);
    }

    /**
     * 修改购物车商品
     *
     * @param mallCart 购物车商品
     * @return 结果
     */
    @Override
    public int updateMallCart(MallCart mallCart) {
        mallCart.setUpdateTime(LocalDateTime.now());
        return baseMapper.updateById(mallCart);
    }

    /**
     * 批量删除购物车商品
     *
     * @param ids 需要删除的购物车商品ID
     * @return 结果
     */
    @Override
    public int deleteMallCartByIds(ReqIdsParam ids) {
        return baseMapper.deleteBatchIds(ids.getId());
    }

    /**
     * 删除购物车商品信息
     *
     * @param id 购物车商品ID
     * @return 结果
     */
    @Override
    public int deleteMallCartById(ReqIdsParam id) {
        return baseMapper.deleteBatchIds(id.getId());
    }

    /**
     * @return
     */
    @Override
    public List<ResMallCartItem> apiList() {
        Long uuid = SecurityUtils.getLoginUser().getAccountLogin().getUuid();
        List<ResMallCartItem> items = baseMapper.apiList(uuid);
        return items;
    }

    @Override
    public void apiUpdate(MallCartEditDTO vo) {
        if (vo.getCartId() == null) {
            MallCart one = lambdaQuery().eq(MallCart::getGoodsId, vo.getGoodsId())
                    .eq(MallCart::getUserUuid, SecurityUtils.getLoginUser().getAccountLogin().getUuid())
                    .eq(MallCart::getNormsId, vo.getNormsId())
                    .one();
            if (one == null) {
                throw new CustomException("购物车信息有误");
            }
            vo.setCartId(one.getId());
        }

        MallCart mallCart = baseMapper.selectForUpdate2(vo.getCartId());
        if (mallCart != null) {
            if (vo.getNormsId() != null) {
                mallCart.setNormsId(vo.getNormsId());
            }
            if (vo.getNumber() != null) {
                if (vo.getNumber() == 0) {
                    baseMapper.deleteById(mallCart.getId());
                    return;
                }
                mallCart.setNumber(vo.getNumber());
            }

            mallCart.setUpdateTime(LocalDateTime.now());
            updateById(mallCart);
        }
    }

    @Override
    public void apiAdd(MallCartAddDTO vo) {
        Long uuid = SecurityUtils.getLoginUser().getAccountLogin().getUuid();
        MallCart mallCart = baseMapper.selectForUpdate3(uuid, vo.getGoodsId(),vo.getNormsId());
        if (mallCart != null) {
            mallCart.setNumber(mallCart.getNumber() + vo.getNumber());
            mallCart.setUpdateTime(LocalDateTime.now());
            baseMapper.updateById(mallCart);
        } else {
            mallCart = new MallCart();
            mallCart.setUserUuid(uuid);
            mallCart.setGoodsId(vo.getGoodsId());
            mallCart.setNormsId(vo.getNormsId());
            mallCart.setNumber(vo.getNumber());
            mallCart.setCreateTime(LocalDateTime.now());
            mallCart.setUpdateTime(LocalDateTime.now());
            baseMapper.insert(mallCart);
        }
    }

    @Override
    public List<MallCart> selectForUpdate(String ids) {
        return baseMapper.selectForUpdate(ids);
    }
}
